﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AmuTools;
using app.Api;

namespace app.Models
{
    [Model(TableName = "t_admin", PrimaryKey = "id", IdentityInsert = false)]
    public class Admin
    {
        public int id { get; set; }

        public string username { get; set; }

        [Field(Webable = false, DataType = "varchar(50)")]
        public string password { get; set; }

        [Field(Webable = false)]
        public int role_id { get; set; }

        private AdminRole _role = null;
        [Field(Storageable = false)]
        public AdminRole role
        {
            get
            {
                if (_role == null)
                {
                    _role = G.DB.GetById<AdminRole>(this.role_id);
                }
                return _role;
            }
            set
            {
                _role = value;
                role_id = _role.id;
            }
        }

        [Field(Webable = false, DataType = "text")]
        public string special_permission { get; set; }

        private string _permission = null;
        [Field(Storageable = false)]
        public string permission {
            get
            {
                if (_permission == null)
                {
                    _permission = (role == null ? "" : role.permission) + this.special_permission;
                }
                return _permission;
            }
        }

        public int status { get; set; }

        [Field(Webable = false)]
        public int is_delete { get; set; }

        [Field(Storageable = false)]
        public int landing_state {
            get
            {
                return (int)Api.LandingServer.GetUnitState(Api.IDENTITY.ADMIN + this.id.ToString());
            }
        }

        [Field(Webable = false)]
        public int is_must { get; set; }

        public static Admin[] GetInitData()
        {
            return new Admin[]{
                new Admin { username = "admin", password = "123456", special_permission = "+100010", is_must = 1 }
            };
        }
    }
    
}